package com.letv.core.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.net.TrafficStats;
import android.text.TextUtils;
import com.letv.component.player.LetvVideoViewBuilder.Type;
import com.letv.core.config.LetvConfig;
import com.letv.core.constant.DownloadConstant;
import com.letv.core.db.PreferencesManager;
import com.letv.datastatistics.DataStatistics;
import com.letv.datastatistics.util.DataConstant;
import com.letv.datastatistics.util.DataUtils;
import com.letv.datastatistics.util.PageIdConstant;
import java.net.URLEncoder;
import java.text.DecimalFormat;

/**
 * 统计
 *
 * @author gaolihua
 */
public class StatisticsUtils {

    // push信息是否含有图片
    private static boolean mPushInfoHasPicture = false;
    // 之前获取的字节数
    private static long mPreviousBytes = 0;
    // 播放器类型
    private static Type mType = null;
    // 当前的动作属性相关信息,启动播放器相关页面时记录
    private static ActionProperty mActionProperty = new ActionProperty();
    // M站跳转启动类型
    private static String mStartType = "";
    // M站跳转来源
    private static String mFrom = "";
    // 播放统计用的ref
    private static String mPlayInfoRef = "";
    // 打开播放页"点击"海报图开始时间
    public static long mClickImageForPlayTime = 0l;
    // 是否是打开应用时（后台进入不算）第一次播放
    private static boolean isFirstPlay = false;

    /**
     * 设置前往播放器相关页面时的动作属性
     *
     * @param fl
     * @param wz
     * @param pageId
     */
    public static void setActionProperty(String fl, int wz, String pageId) {
        if (!TextUtils.isEmpty(fl)) {
            mActionProperty.setFl(fl);
        }
        if (wz >= -1) {
            mActionProperty.setWz(wz);
        }
        if (!TextUtils.isEmpty(pageId)) {
            mActionProperty.setPageId(pageId);
        }
    }

    /**
     * 获取pageid
     *
     * @return
     */
    public static String getPageId() {
        return mActionProperty.getPageId();
    }

    /**
     * 设置pageId
     *
     * @param pageId
     *         不能为空
     */
    public static void setPageId(String pageId) {
        if (!TextUtils.isEmpty(pageId)) {
            mActionProperty.pageId = pageId;
        }
    }

    /**
     * 获取分类
     *
     * @return
     */
    public static String getFl() {
        return mActionProperty.getFl();
    }

    /**
     * 获取位置
     *
     * @return
     */
    public static int getWz() {
        return mActionProperty.getWz();
    }

    public static boolean isPushInfoHasPicture() {
        return mPushInfoHasPicture;
    }

    public static void setPushInfoHasPicture(boolean pushInfoHasPicture) {
        mPushInfoHasPicture = pushInfoHasPicture;
    }

    public static Type getType() {
        return mType;
    }

    public static void setType(Type type) {
        mType = type;
    }

    public static String getStartType() {
        return mStartType;
    }

    public static void setStartType(String startType) {
        mStartType = startType;
    }

    public static String getFrom() {
        return mFrom;
    }

    public static void setFrom(String from) {
        mFrom = from;
    }

    public static String getPlayInfoRef() {
        return mPlayInfoRef;
    }

    /**
     * 设置播放属性相关的ref
     *
     * @param type
     *         0:M站 1:猜你喜欢播放 2:周边视频播放 3:正片推荐播放 4:相关系列播放 5:大tip播放(半屏) 6:大tip播放(全屏), 7:专题播放
     */
    public static void setPlayInfoRef(int type) {
        switch (type) {
            case 0:
                mPlayInfoRef = mFrom;
                break;
            case 1:
                mPlayInfoRef = PageIdConstant.halpPlayPage + "_" + "h214" + "_" + "4";
                break;
            case 2:
                mPlayInfoRef = PageIdConstant.halpPlayPage + "_" + "h212" + "_" + "3";
                break;
            case 3:
                mPlayInfoRef = PageIdConstant.halpPlayPage + "_" + "h214" + "_" + "1";
                break;
            case 4:
                mPlayInfoRef = PageIdConstant.halpPlayPage + "_" + "h214" + "_" + "3";
                break;
            case 5:
                mPlayInfoRef = PageIdConstant.halpPlayPage + "_" + "c683" + "_" + "1";
                break;
            case 6:
                mPlayInfoRef = PageIdConstant.fullPlayPage + "_" + "c683" + "_" + "1";
                break;
            case 7:
                mPlayInfoRef = PageIdConstant.halpPlayPage + "_" + "-" + "_" + "-";
                break;
            default:
                if (!TextUtils.isEmpty(mActionProperty.fl) && !mActionProperty.fl.equals("-") &&
                        mActionProperty.fl.startsWith("http")) {
                    mPlayInfoRef = mActionProperty.fl;
                } else {
                    mPlayInfoRef = mActionProperty.pageId + "_" + mActionProperty.fl + "_" +
                            mActionProperty.wz;
                }
                break;
        }
    }

    /**
     * 获取网速
     *
     * @return
     */
    @SuppressLint("NewApi")
    public static String getSpeed() {
        long totalBytes = TrafficStats.getTotalRxBytes();
        if (totalBytes != TrafficStats.UNSUPPORTED) {
            if (mPreviousBytes == 0) {
                mPreviousBytes = totalBytes;
            } else {
                long networkSpeed = (TrafficStats.getTotalRxBytes() - mPreviousBytes);
                mPreviousBytes = totalBytes;
                return getMbString(networkSpeed);
            }
        }
        return "0";
    }

    /**
     * 将字节数转换成带有相应单位的字符串输出
     *
     * @param bytesize
     * @return
     */
    private static String getMbString(final long bytesize) {

        DecimalFormat df = new DecimalFormat("0.##");
        Double value = Long.valueOf(bytesize).doubleValue();
        Double size = 0.00d;
        String suffix = "";
        if (bytesize == 0) {
            size = 0d;
            suffix = "KB";
        } else if (bytesize < DownloadConstant.SIZE_KB) {
            size = value;
            suffix = "bytes";
        } else if (bytesize >= DownloadConstant.SIZE_KB && bytesize < DownloadConstant.SIZE_MB) {
            size = value / DownloadConstant.SIZE_KB;
            suffix = "KB";
        } else if (bytesize >= DownloadConstant.SIZE_MB && bytesize < DownloadConstant.SIZE_GB) {
            size = value / DownloadConstant.SIZE_MB;
            suffix = "MB";
        } else if (bytesize >= DownloadConstant.SIZE_GB) {
            size = value / DownloadConstant.SIZE_GB;
            suffix = "GB";
        }

        return df.format(size) + suffix + "/S";
    }

    public static boolean isFirstPlay() {
        return isFirstPlay;
    }

    public static void setFirstPlay(boolean isFirstPlay) {
        StatisticsUtils.isFirstPlay = isFirstPlay;
    }

    public static void staticticsInfoPostAddTargetUrl(Context mContext, String acode, String fl,
                                                      String name, int wz, String type,
                                                      String pushtype, String messagetype,
                                                      String msgid, String bid, String pageid,
                                                      String cid, String pid, String vid,
                                                      String curUrl, String zid, String lid,
                                                      String targeturl) {
        staticticsInfoPostAddTargetUrl(mContext.getApplicationContext(), acode, fl, name, wz, type, pushtype, messagetype, msgid, bid, pageid, cid, pid, vid, curUrl, zid, lid, targeturl, 0);
    }

    /**
     * push统计方法(5.5版本)
     *
     * @param mContext
     * @param acode
     * @param fl
     * @param name
     * @param wz
     * @param type
     * @param pushtype
     * @param messagetype
     * @param msgid
     * @param bid
     * @param pageid
     *         推送来源不是自己时，pageid需带上来源
     * @param cid
     * @param pid
     * @param vid
     * @param curUrl
     * @param zid
     * @param lid
     * @param targeturl
     */
    @SuppressWarnings("deprecation")
    public static void staticticsInfoPostAddTargetUrl(Context mContext, String acode, String fl,
                                                      String name, int wz, String type,
                                                      String pushtype, String messagetype,
                                                      String msgid, String bid, String pageid,
                                                      String cid, String pid, String vid,
                                                      String curUrl, String zid, String lid,
                                                      String targeturl, int pfrom) {
        try {
            StringBuilder sb = new StringBuilder();
            if (!TextUtils.isEmpty(fl)) {
                sb.append(
                        DataConstant.StaticticsVersion2Constatnt.StaticticsName.STATICTICS_NAM_FL +
                                fl);
            }
            if (wz >= 0) {
                sb.append("&" +
                        DataConstant.StaticticsVersion2Constatnt.StaticticsName.STATICTICS_NAM_WZ +
                        wz);
            }
            if (!TextUtils.isEmpty(type)) {
                sb.append("&type=" + type);
            }
            if (!TextUtils.isEmpty(pushtype)) {
                sb.append("&pushtype=" + pushtype);
            }
            if (!TextUtils.isEmpty(messagetype)) {
                sb.append("&messagetype=" + messagetype);
            }
            if (!TextUtils.isEmpty(msgid)) {
                sb.append("&msgid=" + msgid);
            }
            if (!TextUtils.isEmpty(name)) {
                sb.append("&" +
                        DataConstant.StaticticsVersion2Constatnt.StaticticsName.STATICTICS_NAM_NA +
                        URLEncoder.encode(name));
            }
            if (!TextUtils.isEmpty(bid)) {
                sb.append("&" +
                        DataConstant.StaticticsVersion2Constatnt.StaticticsName.STATICTICS_NAM_BID +
                        bid);
            }
            if (!TextUtils.isEmpty(pageid)) {
                sb.append("&" +
                        DataConstant.StaticticsVersion2Constatnt.StaticticsName.STATICTICS_NAM_PAGE_ID +
                        pageid);
            }
            sb.append("&ua=" + DataUtils.getDataEmpty(DataUtils.getDeviceName()));
            // zhuqiao 第三方推送
            if (pfrom != 0) {
                sb.append("&pfrom=" + pfrom);
            }
            DataStatistics.getInstance()
                          .sendActionInfo(mContext.getApplicationContext(), "0", "0", LetvConfig.getPcode(mContext
                                          .getApplicationContext()), acode, sb.toString(), "0", cid, pid, vid, LetvUtils
                                          .getUID(), curUrl, null, null, null,
                                  PreferencesManager.getInstance().isLogin() ? 0
                                                                             : 1, zid, lid, targeturl);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 动作统计上报
     *
     * @param mContext
     * @param acode
     * @param fl
     * @param name
     * @param wz
     * @param cid
     * @param pid
     * @param vid
     * @param curUrl
     * @param zid
     */
    public static void staticticsInfoPost(Context mContext, String acode, String fl, String name,
                                          int wz, String cid, String pid, String vid, String curUrl,
                                          String zid) {
        statisticsInfoPost(mContext.getApplicationContext(), acode, fl, name, wz, null, null, cid, pid, vid, curUrl, zid, null, null, null, -1, false);
    }

    /**
     * 动作统计上报
     *
     * @param context
     * @param acode
     * @param fl
     * @param name
     * @param wz
     * @param bid
     * @param pageid
     * @param cid
     * @param pid
     * @param vid
     * @param curUrl
     * @param zid
     */
    public static void staticticsInfoPost(Context context, String acode, String fl, String name,
                                          int wz, String bid, String pageid, String cid, String pid,
                                          String vid, String curUrl, String zid) {
        statisticsInfoPost(context.getApplicationContext(), acode, fl, name, wz, bid, pageid, cid, pid, vid, curUrl, zid, null, null, null, -1, false);
    }

    /**
     * 动作统计上报
     *
     * @param mContext
     * @param acode
     * @param fl
     * @param name
     * @param wz
     * @param bid
     * @param pageid
     * @param cid
     * @param pid
     * @param vid
     * @param curUrl
     * @param zid
     * @param scid
     * @param fragid
     * @param time
     * @param download
     * @param isAddRef
     */
    @SuppressWarnings("deprecation")
    private static void statisticsInfoPost(Context context, String acode, String fl, String name,
                                           int wz, String bid, String pageid, String cid,
                                           String pid, String vid, String curUrl, String zid,
                                           String scid, String fragid, String time, int download,
                                           boolean isAddRef) {
        try {
            StringBuilder sb = new StringBuilder();
            if (!TextUtils.isEmpty(fl)) {
                sb.append(
                        DataConstant.StaticticsVersion2Constatnt.StaticticsName.STATICTICS_NAM_FL +
                                fl);
            }
            if (wz >= 0) {
                sb.append("&" +
                        DataConstant.StaticticsVersion2Constatnt.StaticticsName.STATICTICS_NAM_WZ +
                        wz);
            }
            if (!TextUtils.isEmpty(name)) {
                sb.append("&" +
                        DataConstant.StaticticsVersion2Constatnt.StaticticsName.STATICTICS_NAM_NA +
                        URLEncoder.encode(name));
            }
            if (!TextUtils.isEmpty(bid)) {
                sb.append("&" +
                        DataConstant.StaticticsVersion2Constatnt.StaticticsName.STATICTICS_NAM_BID +
                        bid);
            }
            if (!TextUtils.isEmpty(pageid)) {
                sb.append("&" +
                        DataConstant.StaticticsVersion2Constatnt.StaticticsName.STATICTICS_NAM_PAGE_ID +
                        pageid);
            }
            if (!TextUtils.isEmpty(scid)) {
                sb.append("&scid=" + scid);
            }
            if (!TextUtils.isEmpty(fragid)) {
                sb.append("&fragid=" + fragid);
            }
            if (!TextUtils.isEmpty(time)) {
                sb.append("&time=" + time);
            }
            if (download > 0) {
                sb.append("&" + "download=" + download);
            }
            // 添加ref
            if (isAddRef) {
                StringBuilder sb1 = new StringBuilder();
                if (!TextUtils.isEmpty(pageid)) {
                    sb1.append(pageid);
                }
                if (!TextUtils.isEmpty(fl)) {
                    sb1.append("_" + fl);
                }
                if (wz >= 0) {
                    sb1.append("_" + wz);
                }
                if (!TextUtils.isEmpty(sb1)) {
                    sb.append("&ref=" + sb1.toString());
                }
            }
            LogInfo.LogStatistics("StatisticsUtils-action:" + sb.toString());
            DataStatistics.getInstance()
                          .sendActionInfo(context.getApplicationContext(), "0", "0", LetvConfig.getPcode(context
                                          .getApplicationContext()), acode, sb.toString(), "0", cid, pid, vid, LetvUtils
                                          .getUID(), curUrl, null, null, null,
                                  PreferencesManager.getInstance().isLogin() ? 0 : 1, zid);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 动作统计上报(5.5版本),新增ref参数
     *
     * @param mContext
     * @param acode
     * @param fl
     * @param name
     * @param wz
     * @param bid
     * @param pageid
     * @param cid
     * @param pid
     * @param vid
     * @param curUrl
     * @param zid
     */
    public static void staticticsInfoPostAddRef(Context mContext, String acode, String fl,
                                                String name, int wz, String bid, String pageid,
                                                String cid, String pid, String vid, String curUrl,
                                                String zid) {
        statisticsInfoPost(mContext, acode, fl, name, wz, bid, pageid, cid, pid, vid, curUrl, zid, null, null, null, -1, true);
    }

    /**
     * 动作统计上报(5.5版本),新增scid参数
     *
     * @param mContext
     * @param acode
     * @param fl
     * @param name
     * @param wz
     * @param bid
     * @param pageid
     * @param cid
     * @param pid
     * @param vid
     * @param curUrl
     * @param zid
     * @param scid
     */
    public static void staticticsInfoPostAddScid(Context mContext, String acode, String fl,
                                                 String name, int wz, String bid, String pageid,
                                                 String cid, String pid, String vid, String curUrl,
                                                 String zid, String scid) {
        statisticsInfoPost(mContext, acode, fl, name, wz, bid, pageid, cid, pid, vid, curUrl, zid, scid, null, null, -1, false);
    }

    /**
     * 动作统计上报(5.5.1版本),新增fragid(碎片id)参数(为频道页添加)
     *
     * @param mContext
     * @param acode
     * @param fl
     *         分类
     * @param name
     *         模块名
     * @param wz
     *         位置
     * @param bid
     *         模块id
     * @param pageid
     *         页面id
     * @param cid
     *         频道id
     * @param pid
     *         专辑id
     * @param vid
     *         视频id
     * @param curUrl
     *         当前页面地址
     * @param zid
     *         专题id
     * @param scid
     *         接口中提供的pageid的值
     * @param fragid
     *         接口中提供的碎片的id(频道用)
     */
    public static void staticticsInfoPostAddFragid(Context mContext, String acode, String fl,
                                                   String name, int wz, String bid, String pageid,
                                                   String cid, String pid, String vid,
                                                   String curUrl, String zid, String scid,
                                                   String fragid) {
        statisticsInfoPost(mContext, acode, fl, name, wz, bid, pageid, cid, pid, vid, curUrl, zid, scid, fragid, null, -1, false);
    }

    /**
     * add time
     *
     * @param mContext
     * @param acode
     * @param fl
     * @param name
     * @param wz
     * @param bid
     * @param pageid
     * @param cid
     * @param pid
     * @param vid
     * @param curUrl
     * @param zid
     * @param time
     */
    public static void staticticsInfoPostAddTime(Context mContext, String acode, String fl,
                                                 String name, int wz, String bid, String pageid,
                                                 String cid, String pid, String vid, String curUrl,
                                                 String zid, String time) {
        statisticsInfoPost(mContext, acode, fl, name, wz, bid, pageid, cid, pid, vid, curUrl, zid, null, null, time, -1, false);
    }

    /**
     * push统计
     *
     * @param mContext
     * @param acode
     * @param fl
     * @param name
     * @param wz
     * @param download
     * @param bid
     * @param pageid
     * @param cid
     * @param pid
     * @param vid
     * @param curUrl
     * @param zid
     */
    public static void staticticsInfoPostAddDownload(Context mContext, String acode, String fl,
                                                     String name, int wz, int download, String bid,
                                                     String pageid, String cid, String pid,
                                                     String vid, String curUrl, String zid) {
        statisticsInfoPost(mContext, acode, fl, name, wz, bid, pageid, cid, pid, vid, curUrl, zid, null, null, null, download, false);
    }

    /**
     * 动作统计上报(5.5.1版本),新增fragid(碎片id)参数(首页)
     *
     * @param mContext
     * @param acode
     * @param fl
     * @param name
     * @param wz
     * @param bid
     * @param pageid
     * @param cid
     * @param pid
     * @param vid
     * @param curUrl
     * @param zid
     * @param lid
     * @param fragid
     */
    public static void staticticsInfoPostAddLidFragId(Context mContext, String acode, String fl,
                                                      String name, int wz, String bid,
                                                      String pageid, String cid, String pid,
                                                      String vid, String curUrl, String zid,
                                                      String lid, String fragid) {
        staticticsInfoPost(mContext, acode, fl, name, wz, bid, pageid, cid, pid, vid, curUrl, zid, lid, fragid);
    }

    /**
     * 动作统计上报-增加lid参数
     *
     * @param mContext
     * @param acode
     * @param fl
     * @param name
     * @param wz
     * @param bid
     * @param pageid
     * @param cid
     * @param pid
     * @param vid
     * @param curUrl
     * @param zid
     * @param lid
     */
    public static void staticticsInfoPostAddLid(Context mContext, String acode, String fl,
                                                String name, int wz, String bid, String pageid,
                                                String cid, String pid, String vid, String curUrl,
                                                String zid, String lid) {
        staticticsInfoPost(mContext, acode, fl, name, wz, bid, pageid, cid, pid, vid, curUrl, zid, lid, null);
    }

    /**
     * 直播动作统计上报
     *
     * @param mContext
     * @param acode
     * @param fl
     * @param name
     * @param wz
     * @param bid
     * @param pageid
     * @param cid
     * @param pid
     * @param lid
     * @param curUrl
     * @param zid
     */
    public static void staticticsInfoPostAddLidDelVid(Context mContext, String acode, String fl,
                                                      String name, int wz, String bid,
                                                      String pageid, String cid, String pid,
                                                      String lid, String curUrl, String zid) {
        staticticsInfoPost(mContext, acode, fl, name, wz, bid, pageid, cid, pid, null, curUrl, zid, lid, null);
    }

    /**
     * 动作统计上报-增加lid, fragid参数
     *
     * @param mContext
     * @param acode
     * @param fl
     * @param name
     * @param wz
     * @param bid
     * @param pageid
     * @param cid
     * @param pid
     * @param vid
     * @param curUrl
     * @param zid
     * @param lid
     * @param fragid
     */
    @SuppressWarnings("deprecation")
    private static void staticticsInfoPost(Context mContext, String acode, String fl, String name,
                                           int wz, String bid, String pageid, String cid,
                                           String pid, String vid, String curUrl, String zid,
                                           String lid, String fragid) {
        try {
            StringBuilder sb = new StringBuilder();
            if (!TextUtils.isEmpty(fl)) {
                sb.append(
                        DataConstant.StaticticsVersion2Constatnt.StaticticsName.STATICTICS_NAM_FL +
                                fl);
            }
            if (wz >= 0) {
                sb.append("&" +
                        DataConstant.StaticticsVersion2Constatnt.StaticticsName.STATICTICS_NAM_WZ +
                        wz);
            }
            if (!TextUtils.isEmpty(name)) {
                sb.append("&" +
                        DataConstant.StaticticsVersion2Constatnt.StaticticsName.STATICTICS_NAM_NA +
                        URLEncoder.encode(name));
            }
            if (!TextUtils.isEmpty(bid)) {
                sb.append("&" +
                        DataConstant.StaticticsVersion2Constatnt.StaticticsName.STATICTICS_NAM_BID +
                        bid);
            }
            if (!TextUtils.isEmpty(pageid)) {
                sb.append("&" +
                        DataConstant.StaticticsVersion2Constatnt.StaticticsName.STATICTICS_NAM_PAGE_ID +
                        pageid);
            }
            if (!TextUtils.isEmpty(fragid)) {
                sb.append("&fragid=" + fragid);
            }
            LogInfo.LogStatistics("StatisticsUtils-action:" + sb.toString());
            DataStatistics.getInstance()
                          .sendActionInfo(mContext, "0", "0", LetvConfig.getPcode(mContext.getApplicationContext()), acode, sb
                                          .toString(), "0", cid, pid, vid, LetvUtils.getUID(), curUrl, null, null, null,
                                  PreferencesManager.getInstance().isLogin() ? 0 : 1, zid, lid);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void staticticsInfoPost(Context mContext, String fl, String name, int wz, int id,
                                          String cid, String pid, String vid, String curUrl,
                                          String zid) {
        try {
            StringBuilder sb = new StringBuilder();
            if (!TextUtils.isEmpty(fl)) {
                sb.append(
                        DataConstant.StaticticsVersion2Constatnt.StaticticsName.STATICTICS_NAM_FL +
                                fl);
            }
            if (wz >= 0) {
                sb.append("&" +
                        DataConstant.StaticticsVersion2Constatnt.StaticticsName.STATICTICS_NAM_WZ +
                        (wz + 1));
            }
            if (!TextUtils.isEmpty(name)) {
                sb.append("&" +
                        DataConstant.StaticticsVersion2Constatnt.StaticticsName.STATICTICS_NAM_NA +
                        name);
            }
            if (id > 0) {
                sb.append("&" +
                        DataConstant.StaticticsVersion2Constatnt.StaticticsName.STATICTICS_NAM_CID +
                        id);
            }
            LogInfo.LogStatistics("StatisticsUtils-action:" + sb.toString());
            DataStatistics.getInstance()
                          .sendActionInfo(mContext, "0", "0", LetvConfig.getPcode(mContext.getApplicationContext()), "0", sb
                                          .toString(), "0", cid, pid, vid, LetvConfig.getPcode(mContext.getApplicationContext()), curUrl, null, null, null,
                                  PreferencesManager.getInstance().isLogin() ? 0 : 1, zid);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * app启动上报(正常/push/后台)
     *
     * @param type1
     *         触发启动到展示内容的时长
     * @param type2
     *         广告位时长
     * @param time
     *         启动时长；产品开机图到开机广告图的结束的时长
     * @param launchType
     *         1:正常 2:push 3:后台
     */
    public static void statisticsAppLaunch(Context context, String type1, String type2, String time,
                                           int launchType, String pageId) {
        StringBuilder sb = new StringBuilder();
        sb.append("&type1=").append(type1);
        sb.append("&type2=").append(type2);
        sb.append("&time=").append(time);
        sb.append("&launchType=").append(launchType);
        sb.append("&pageid=").append(pageId);
        // 上传统计
        DataStatistics.getInstance()
                      .sendActionInfo(context, "0", "0", LetvConfig.getPcode(context.getApplicationContext()), "11", sb
                                      .toString(), "0", null, null, null, LetvUtils.getUID(), null, null, null, null,
                              PreferencesManager.getInstance().isLogin() ? 0 : 1, null);
    }

    /**
     * 登录统计
     *
     * @param context
     * @param st
     */
    public static void statisticsLogin(Context context, int st) {
        // long currentTime = time;
        // long timeElasp = (currentTime - BaseApplication.getInstance().getAppStartTime()) / 1000;
        // String loginProperty = "usertype=-&onlen=";
        // if (st == 0) {
        // loginProperty += "-";
        // } else {
        // loginProperty += ((BaseApplication.getInstance().getAppStartTime() == 0 || timeElasp <= 0) ? "" : timeElasp);
        // }
        String loginProperty = "";
        DataStatistics.getInstance()
                      .sendLoginInfo(context.getApplicationContext(), "0", "0", LetvUtils.getUID(), loginProperty, "-",
                              System.currentTimeMillis() / 1000 +
                                      "", LetvConfig.getPcode(context.getApplicationContext()), st, null);
        LogInfo.LogStatistics("login上报..." + loginProperty);
    }

    public static void statisticsLogin(Context context, int st, String ref) {
        // long currentTime = time;
        // long timeElasp = (currentTime - BaseApplication.getInstance().getAppStartTime()) / 1000;
        // String loginProperty = "usertype=-&onlen=";
        // if (st == 0) {
        // loginProperty += "-";
        // } else {
        // loginProperty += ((BaseApplication.getInstance().getAppStartTime() == 0 || timeElasp <= 0) ? "" : timeElasp);
        // }
        String loginProperty = "";
        DataStatistics.getInstance()
                      .sendLoginInfo(context.getApplicationContext(), "0", "0", LetvUtils.getUID(), loginProperty, ref,
                              System.currentTimeMillis() / 1000 +
                                      "", LetvConfig.getPcode(context.getApplicationContext()), st, null);
        LogInfo.LogStatistics("login上报..." + loginProperty);
    }

    /**
     * 环境日志统计
     *
     * @param context
     */
    public static void statisticsEnv(Context context) {
        DataStatistics.getInstance()
                      .sendEnvInfo(context.getApplicationContext(), "0", "0", "", LetvConfig.getSource(), null, "", "");
    }

    /**
     * 提交未成功上报的统计数据
     */
    public static void submitLocalErrors(final Context context) {
        final Context mContext = context.getApplicationContext();
        new Thread(new Runnable() {

            @Override
            public void run() {
                DataStatistics.getInstance().submitErrorInfo(mContext.getApplicationContext());
            }
        }).start();
    }

    /**
     * 设置页面-上报各开关属性(Version 5.9)
     *
     * @param context
     */
    public static void statisticsSettings(Context context) {
        StringBuffer sb = new StringBuffer();
        String sk; // 是否跳过片头片尾
        String nt; // 运行商网络缓存
        String ps; // 接收推送消息
        String sh;// 摇一摇随便看
        if (PreferencesManager.getInstance().isSkip()) {
            sk = "1";
            sb.append("sk=" + sk);
        } else {
            sk = "0";
            sb.append("sk=" + sk);
        }
        if (PreferencesManager.getInstance().isAllowMobileNetwork()) {
            nt = "1";
            sb.append("&nt=" + nt);
        } else {
            nt = "0";
            sb.append("&nt=" + nt);
        }
        if (PreferencesManager.getInstance().isPush()) {
            ps = "1";
            sb.append("&ps=" + ps);
        } else {
            ps = "0";
            sb.append("&ps=" + ps);
        }
        if (PreferencesManager.getInstance().isShack()) {
            sh = "1";
            sb.append("&sh=" + sh);
        } else {
            sh = "0";
            sb.append("&sh=" + sh);
        }
        String cacheSize = "0.00M";
        try {
            cacheSize = LetvUtils.getCacheSize(context);
        } catch (Exception e) {
            e.printStackTrace();
        }
        sb.append("&cl=" + cacheSize);
        sb.append("&fl=91").append("&pageid=" + PageIdConstant.searchResultPage);
        LogInfo.LogStatistics("settings:" + sb.toString());
        DataStatistics.getInstance()
                      .sendActionInfo(context.getApplicationContext(), "0", "0", LetvConfig.getPcode(context
                                      .getApplicationContext()), "19", sb.toString(), "0", null, null, null, LetvUtils
                                      .getUID(), null, null, null, null,
                              PreferencesManager.getInstance().isLogin() ? 0 : 1, null);
    }

    /**
     * 重置统计相关变量
     */
    public static void clear() {
        mActionProperty.setFl("");
        mActionProperty.setWz(-1);
        mActionProperty.setPageId("");
        mPushInfoHasPicture = false;
        mPreviousBytes = 0;
        mType = null;
        mStartType = "";
        mFrom = "";
        mPlayInfoRef = "";
        mClickImageForPlayTime = 0l;
        isFirstPlay = false;
        DataStatistics.setUuid("");
    }

    /**
     * 动作属性
     *
     * @author jiancong
     */
    public static class ActionProperty {

        // 分类
        private String fl = "";
        // 位置
        private int wz = -1;
        // 页面id
        private String pageId = "";

        public String getFl() {
            return fl;
        }

        public void setFl(String fl) {
            this.fl = fl;
        }

        public int getWz() {
            return wz;
        }

        public void setWz(int wz) {
            this.wz = wz;
        }

        public String getPageId() {
            return pageId;
        }

        public void setPageId(String pageId) {
            this.pageId = pageId;
        }
    }

    /**
     * 播放页面相关的信息
     *
     * @author jiancong
     */
    public static class StatisticsPlayInfo {

        /** 广告实际时长 */
        public long mAdConsumeTime = 0;
        /** 广告接口数据请求时长 **/
        public long mAdsRequestTime = 0;
        /** 广告拼接时长（不一定有） **/
        public long mAdsJoinTime = 0;
        /** 广告加载时长（广告数据解析、拼接完到第一帧画面的时长） **/
        public long mAdsLoadConsumeTime = 0;
        /** 广告总时长 **/
        public long mAdsToalTime = 0;
        /** 广告第一帧播放时间 **/
        public long mAdsPlayFirstFrameTime = 0;
        /** 正片加载时长（拿到正片播放地址导出现第一帧画面的时长） **/
        public long mVideoLoadConsumeTime = 0;
        /** 起播时长(点击开始到广告或者正片第一帧) **/
        public long mTotalConsumeTime = 0;
        /** 广告url */
        public String mAdUrl = "";
    }

}
